package com.ninjia.api.controller;

import com.ninjia.common.pojo.CommonErrorDict;
import com.ninjia.common.pojo.ErrorDict;
import com.ninjia.core.constants.CoreErrorDict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ninjia.common.pojo.APIResult;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by xiabin on 2017/7/17.
 */
@ControllerAdvice
public class ExceptionController {
    /**
     * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {}

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public APIResult defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        return new APIResult(CommonErrorDict.SystemError.res());
    }
    /*
    public static final String DEFAULT_ERROR_VIEW = "error";
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        return mav;
    }
    @ExceptionHandler(value = JsonRetException.class)
    @ResponseBody
    public BaseResp<Null> APIErrorHandder(HttpServletRequest req, JsonRetException e) throws Exception {
        return new BaseResp<>(e.getLocalmsg().getErrorCode(), e.getLocalmsg().getErrorMsg(), null);
    }*/
}
